package com.tchepannou.limbe.gae.converter;

import com.google.appengine.api.datastore.Text;
import com.google.gson.Gson;
import com.tchepannou.limbe.annotation.Embed;
import com.tchepannou.util.converter.Converter;

public class TextConverter
    implements Converter
{
    //-- Private
    private Gson _gson = new Gson();


    //-- Converter overrides
    @Override
    public Object convert(Object value)
    {
        if (value == null)
        {
            return null;
        }
        else if (value instanceof String)
        {
            return new Text((String)value);
        }
        else if (value instanceof Text)
        {
            return value;
        }
        else if (value.getClass().getAnnotation(Embed.class) != null)
        {
            String json = _gson.toJson(value);
            return new Text (json);
        }
        else
        {
            return null;
        }
    }
}
